
using System;
using System.Data;
using System.Data.SqlClient;
using Csla;
using Csla.Data;
using Csla.Validation;
using SariturIntranetLibrary.RM;

namespace SariturIntranetLibrary.Alertas
{
    [Serializable()]
    public class AlertaChildInfo : Csla.ReadOnlyBase<AlertaChildInfo>
    {

        #region Business Properties and Methods

        //declare members

        private Guid _id = Guid.Empty;

        private Guid _destinatarioId = Guid.Empty;

        private Guid _remetenteId = Guid.Empty;

        private Funcionario _remetenteFuncionario;

        private string _setor = string.Empty;

        private SmartDate _dataCriacao = new SmartDate(false);

        private SmartDate _dataVisualizacao = new SmartDate(false);

        private int _tipoDeAlertaId = 0;

        private string _tipoDeAlertaString = string.Empty;

        private int _tipoStatusId = 0;

        private string _statusString = string.Empty;

        private string _titulo = string.Empty;

        private string _descricao = string.Empty;

        private string _destinatarioCPF = string.Empty;

        public Guid Id
        {
            get
            {
                CanReadProperty("Id", true);
                return _id;
            }
        }

        public Guid DestinatarioId
        {
            get
            {
                CanReadProperty("DestinatarioId", true);
                return _destinatarioId;
            }
        }

        public Guid RemetenteId
        {
            get
            {
                CanReadProperty("RemetenteId", true);
                return _remetenteId;
            }
        }

        public string Setor
        {
            get
            {
                CanReadProperty("Setor", true);
                return _setor;
            }
        }

        public DateTime DataCriacao
        {
            get
            {
                CanReadProperty("DataCriacao", true);
                return _dataCriacao.Date;
            }
        }

        public string DataCriacaoString
        {
            get
            {
                CanReadProperty("DataCriacaoString", true);
                return _dataCriacao.Text;
            }
        }

        public DateTime DataVisualizacao
        {
            get
            {
                CanReadProperty("DataVisualizacao", true);
                return _dataVisualizacao.Date;
            }
        }

        public string DataVisualizacaoString
        {
            get
            {
                CanReadProperty("DataVisualizacaoString", true);
                return _dataVisualizacao.Text;
            }
        }

        public int TipoDeAlertaId
        {
            get
            {
                CanReadProperty("TipoDeAlertaId", true);
                return _tipoDeAlertaId;
            }
        }

        public string TipoDeAlertaString
        {
            get
            {
                CanReadProperty("TipoDeAlertaString", true);
                return _tipoDeAlertaString;
            }
        }

        public int TipoStatusId
        {
            get
            {
                CanReadProperty("TipoStatusId", true);
                return _tipoStatusId;
            }
        }

        public string StatusString
        {
            get
            {
                CanReadProperty("StatusString", true);
                return _statusString;
            }
        }

        public string Titulo
        {
            get
            {
                CanReadProperty("Titulo", true);
                return _titulo;
            }
        }

        public string Descricao
        {
            get
            {
                CanReadProperty("Descricao", true);
                return _descricao;
            }
        }

        public Funcionario RemetenteFuncionario
        {
            get
            {
                CanReadProperty("RemetenteFuncionario", true);
                return _remetenteFuncionario;
            }
        }

        public string DescricaoLimite
        {
            get
            {
                CanReadProperty("DescricaoLimite", true);
                if (_descricao.Length > 150)
                    return _descricao.Substring(0, 150) + " (...)";
                return _descricao;
            }
        }

        public string DescricaoLimiteHome
        {
            get
            {
                CanReadProperty("DescricaoLimiteHome", true);
                if (_descricao.Length > 60)
                    return _descricao.Substring(0, 59) + " (...)";
                return _descricao;
            }
        }

        [System.ComponentModel.DataObjectField(true, false)]
        public string DestinatarioCPF
        {
            get
            {
                CanReadProperty("DestinatarioCPF", true);
                return _destinatarioCPF;
            }
        }

        protected override object GetIdValue()
        {
            return _destinatarioCPF;
        }

        #endregion //Business Properties and Methods

        #region Authorization Rules
        protected override void AddAuthorizationRules()
        {
            //TODO: Define authorization rules in AlertaChildInfo
            //AuthorizationRules.AllowRead("Id", "AlertaChildInfoReadGroup");
            //AuthorizationRules.AllowRead("DestinatarioId", "AlertaChildInfoReadGroup");
            //AuthorizationRules.AllowRead("RemetenteId", "AlertaChildInfoReadGroup");
            //AuthorizationRules.AllowRead("Setor", "AlertaChildInfoReadGroup");
            //AuthorizationRules.AllowRead("DataCriacao", "AlertaChildInfoReadGroup");
            //AuthorizationRules.AllowRead("DataCriacaoString", "AlertaChildInfoReadGroup");
            //AuthorizationRules.AllowRead("DataVisualizacao", "AlertaChildInfoReadGroup");
            //AuthorizationRules.AllowRead("DataVisualizacaoString", "AlertaChildInfoReadGroup");
            //AuthorizationRules.AllowRead("TipoDeAlertaId", "AlertaChildInfoReadGroup");
            //AuthorizationRules.AllowRead("TipoDeAlertaString", "AlertaChildInfoReadGroup");
            //AuthorizationRules.AllowRead("TipoStatusId", "AlertaChildInfoReadGroup");
            //AuthorizationRules.AllowRead("StatusString", "AlertaChildInfoReadGroup");
            //AuthorizationRules.AllowRead("Titulo", "AlertaChildInfoReadGroup");
            //AuthorizationRules.AllowRead("Descricao", "AlertaChildInfoReadGroup");
            //AuthorizationRules.AllowRead("DestinatarioCPF", "AlertaChildInfoReadGroup");

        }

        #endregion //Authorization Rules

        #region Factory Methods
        internal static AlertaChildInfo GetAlertaChildInfo(SafeDataReader dr)
        {
            return new AlertaChildInfo(dr);
        }

        private AlertaChildInfo(SafeDataReader dr)
        {
            Fetch(dr);
        }

        internal void AtualizarObject()
        {
            _remetenteFuncionario = Funcionario.GetFuncionario(Usuarios.Usuario.GetUsuario(_remetenteId).Cpf);
        }
        #endregion //Factory Methods
        
        #region Data Access

        #region Data Access - Fetch
        private void Fetch(SafeDataReader dr)
        {
            FetchObject(dr);

            //load child object(s)
            FetchChildren(dr);

        }

        private void FetchObject(SafeDataReader dr)
        {
            _id = dr.GetGuid("Id");
            _destinatarioId = dr.GetGuid("DestinatarioId");
            _remetenteId = dr.GetGuid("RemetenteId");
            _setor = dr.GetString("Setor");
            _dataCriacao = dr.GetSmartDate("DataCriacao", _dataCriacao.EmptyIsMin);
            _dataVisualizacao = dr.GetSmartDate("DataVisualizacao", _dataVisualizacao.EmptyIsMin);
            _tipoDeAlertaId = dr.GetInt32("TipoDeAlertaId");
            _tipoDeAlertaString = dr.GetString("TipoDeAlertaString");
            _tipoStatusId = dr.GetInt32("TipoStatusId");
            _statusString = dr.GetString("StatusString");
            _titulo = dr.GetString("Titulo");
            _descricao = dr.GetString("Descricao");
            _destinatarioCPF = dr.GetString("DestinatarioCPF");
        }

        private void FetchChildren(SafeDataReader dr)
        {
        }
        #endregion //Data Access - Fetch
        #endregion //Data Access
    }
}
